
// pages/patrolLog/patrolLog.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({

  /**
   * 页面的初始数据 
   */
  data: {
    isLocationShow: false,
    isEquipTypeShow: false,
    locationArray: ['门诊', '急诊', '住院部'],
    locationIndex: 0,
    equip_ver: 'G25-2',
    equipType: 0,//1轮椅，2担架
    boxType: '',//string
    equipTypes: [{ value: '1', name: '轮椅', checked: false }, { value: '2', name: '担架', checked: false }],
    boxTypes: [{ name: 'V2.1短钩' }, { name: 'V2.1长钩' }, { name: 'V3.0长钩' }, { name: 'V3.0短钩', checked: true}],
    equ_lock_types: [{//设备锁 长钩 短钩
      value: '长钩',
    }, {
      value: '短钩',
    }],
    equ_lock_type: '', //string
  },

  onLoad: function (options) {
    let that = this;
    console.log("options",options)
    that.setData({
      hospitalId: options.hospitalId,
      hospitalName: options.hospitalName,
      pileCode: options.pileCode,
      feeRules: JSON.parse(options.feeRules),
      scrollHeight: app.getScrollHeight(45),
      equipTypes: app.globalData.config.typeList
    })
    if (options.equipCode){
      let equipType = options.equipType == '轮椅' ? 1 : 2
      that.setData({
        equipCode: options.equipCode,
        equipType: equipType
      })
    }
    if (options.eqtModelName && options.eqtModelName!='undefined') {
      that.setData({
        equip_ver: options.eqtModelName
      })
    }
    that.getLocationList()
    that.getEquVer()
    that.getPileInfo();
  },

  onReady: function () {
    
    let that = this;
    that.toast = that.selectComponent("#toast");
    that.personalModal = that.selectComponent("#personalModal");
  },

  onShow: function () {

  },

  onHide: function () {

  },

  onUnload: function () {

  }, 
  /**
   * 获取桩信息
   */
  getPileInfo: function(){
    let that = this;
    lbbService.getPileDetailInfo({
      hid: parseInt(that.data.pileCode)
    }, res => {
      that.setData({
        pileInfo:res
      })
    })
  },
  getEquVer: function(){
    let that = this
    let equ_vers = []
    lbbService.getEquVer({}, function (res) {
      Object.keys(res.data).forEach(function (key) {
        equ_vers.push({ value: res.data[key], checked: res.data[key] == that.data.equip_ver })
      })
      that.setData({
        equ_vers: equ_vers
      })
    })
  },
  /**
   * 设备锁类型变化：长钩/短钩
   */
  equLockTypeChange: function(e){
    let that = this
    let equ_lock_type = e.detail.value
    that.setData({
      equ_lock_type: equ_lock_type
    })
  },
  equVerChange: function (e) {
    let that = this
    console.log(e)
    let equip_ver = e.detail.value
    that.setData({
      equip_ver
    })
  },
  // 选择位置的显示与隐藏
  showLocationModal: function () {
    this.setData({
      isLocationShow: true
    })
  },
  hideLocationModal: function () {
    this.setData({
      isLocationShow: false
    })
  },

  stopMaoPao: function () {
    return
  },

  //选择位置
  chooseLocation: function (e) {
    let that = this
    that.setData({
      locationIndex: e.currentTarget.dataset.location,
    })
    that.hideLocationModal()
  },
  //新增位置
  addLocation: function (e) {
    console.log(e)
    let that = this
    let choosedlocation = e.detail.value.location
    if (!choosedlocation){
      
      that.toast.showToast('请输入内容');
      setTimeout(function(){
        that.hideLocationModal()
      },2500)
      return
    }
    lbbService.addPosByHos({
      hospitalId: parseInt(that.data.hospitalId),
      position: choosedlocation},function(res){
        that.hideLocationModal()
        that.getLocationList()
      })
  },
  equipTypeChange: function(e){
    console.log(e.detail.value)
    this.setData({
      equipType: e.detail.value
    })
  },
  boxTypeChange: function (e) {
    util.print('boxTypeChange',e);
    let that = this;
    that.data.pileInfo.hpLockType = e.detail.value;
    that.setData({
      pileInfo: that.data.pileInfo
    })
  },
  equVerChange: function (e) {
    let that = this
    console.log(e)
    let equip_ver = e.detail.value
    that.setData({
      equip_ver
    })
  },
  //提交
  formSubmit: function (e) {
    console.log(e)        
    var that = this;
    var formData = e.detail.value;
    let equipCode = 0
    if (that.data.equipCode){
      equipCode = that.data.equipCode
    }
    if (that.data.equipType == 0) {
      that.toast.showToast('请选择桩类型');
      return
    }
    if (!that.data.pileInfo.hpLockType) {
      that.toast.showToast('请选择控制盒类型');
      return
    }
    // if (that.data.equipCode && that.data.equipType==1 && !that.data.equ_lock_type) {
    //   that.toast.showToast('请选择设备类型');
    //   return
    // }
    if (that.data.equipCode && that.data.equipType==1 && !that.data.equip_ver) {
      that.toast.showToast('请选择设备型号');
      return
    }
    lbbService.addPileAndEquipment({
        hospitalId: parseInt(that.data.hospitalId),
        pileCode: parseInt(that.data.pileCode),
        pileLocation: that.data.locationArray[that.data.locationIndex],
        equipCode: parseInt(equipCode),
        eqtModelName: that.data.equip_ver,
        eqtLockType: that.data.pileInfo.hpLockType,
        equipType: parseInt(that.data.equipType),
        hpLockType: that.data.pileInfo.hpLockType,
        hpVerVal: that.data.pileInfo.hpVerVal,
        poleStatus: 1
      },function(res){
        that.toast.showToast('新增成功','success');
        setTimeout(function () {
          wx.navigateBack({//返回
            delta: 1
          })
        }, 2000);
      })
  },
  getLocationList: function () {
    let that = this
    lbbService.getPosByHos({ hospitalId: parseInt(that.data.hospitalId) }, function (res) {
      that.setData({
        locationArray: res.data
      })
      that.setData({
        locationIndex: that.data.locationArray.length - 1
      })
    })
  },
})